/*-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License.You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
	
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------*/

package fever.structures.colors 
{
	import fever.structures.Cloneable;
	import fever.structures.IColor;
	import fever.utils.ColorSpace;
	import fever.utils.Stringifier;	

	/**
	 * Defines color using HSV componants and range.
	 * 
	 * <p>You have to define your HSV Range before any HSV instanciation, 
	 * take a look at example below.</p>
	 * 
	 * @exemple
	 * <listing version="3.0">
	 * 
	 *   ColorSpace.HSVRANGE = HSVRange.KDE;
	 *   
	 *   var color : HSV = new HSV( 100, 100, 100 );
	 * }
	 * </listing>
	 * 
	 * @see fever.structures.IColor
	 * @see fever.structures.colors.HSVRange
	 * @see fever.utils.ColorSpace
	 * 
	 * @author Romain Ecarnot
	 */
	public class HSV implements IColor, Cloneable
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------

		private var _hue : int;
		private var _saturation : int;
		private var _value : int;
		private var _range : HSVRange;

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/** Hue value. */
		public function get hue() : int 
		{ 
			return _hue; 
		}

		/** @private */
		public function set hue( n : int ) : void 
		{ 
			_setHue( n ); 
		}		

		/** Saturation value. */
		public function get saturation() : int 
		{ 
			return _saturation; 
		}

		/** @private */
		public function set saturation( n : int ) : void 
		{ 
			_setSaturation( n ); 
		}

		/** Luminance value. */
		public function get value() : int 
		{ 
			return _value; 
		}

		/** @private */
		public function set value( n : int ) : void 
		{ 
			_setValue( n ); 
		}

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 * 
		 * @param h Hue value
		 * @param s Saturation value
		 * @param v Color value
		 */
		public function HSV( h : Number, s : Number, v : Number )
		{
			_range = ColorSpace.HSVRANGE;
			
			_setHue( h );
			_setSaturation( s );
			_setValue( v );
		}		

		/** 
		 * Returns color value in Number format.
		 */
		public function getColor() : uint
		{
			return ColorSpace.hsvToHexa( this );
		}

		/**
		 * Return color value in hexa String format.
		 */ 
		public function getString() : String
		{
			return ColorSpace.hsvToString( this );
		}

		/**
		 * Returns a clone instance.
		 * 
		 * @return Cloneable
		 */
		public function clone() : Cloneable
		{
			return new HSV( _hue, _saturation, _value );
		}

		/**
		 * Returns string representation.
		 */
		public function toString() : String
		{
			return Stringifier.parse( this, "HSV [" + _hue + "." + _saturation + "." + _value + "]" );
		}

		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
				
		private function _setHue( n : int ) : void
		{
			if( n >= _range.hue.min && n <= _range.hue.max ) 
				_hue = n;
			else _hue = 0;
		}

		private function _setSaturation( n : int ) : void
		{
			if( n >= _range.saturation.min && n <= _range.saturation.max ) 
				_saturation = n;
			else _saturation = 0;
		}

		private function _setValue( n : int ) : void
		{
			if( n >= _range.value.min && n <= _range.value.max ) 
				_value = n;
			else _value = 0;
		}
	}
}
